import { Component } from "@angular/core";
import { NavController, NavParams, AlertController } from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { RepairInfoBean } from "../../repair-info/bean/repair-info.bean";
import * as moment from "moment";
import { RepairListPage} from "../repair-list/repair-list";
import {ApplyRepairPage} from "../../repair-apply/apply-repair";

/**
 * 维修确认详情页面
 */
@Component({
  templateUrl: "repair-detail.html"
})
export class RepairDetailPage {
  private repairInfoBean: RepairInfoBean = new RepairInfoBean();
  private noticeBean: NoticeDetailBean = new NoticeDetailBean();
  repairInfoId : string;
  private imageList: any = [];
  buildAddr : string; //获取电梯安装地址
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private httpService: HttpService,
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private storage: Storage,
  ) {
    this.repairInfoId = this.navParams.get("repairInfoId");
    this.storage.get("user").then(data => {
      //this.repairInfoBean = this.navParams.get("repairInfo");
      this.repairInfoBean.confirmUserId = data.id;
    });

      this.httpService.queryRepairInfoById({repairInfoId:this.repairInfoId}).subscribe(data =>{
          if (data.status == 200) {
              this.repairInfoBean = data.data;
              //获取电梯的安装地址
              this.httpService.queryElevatorInfo({ id: this.repairInfoBean.elevatorId }).subscribe(data =>{
                  if(data.status == 200){
                      this.buildAddr = data.data.buildAddr;
                  }
              })
          }else {
              alert("获取维修确认信息失败");
          }
      });

      let id = this.navParams.get("id");
      this.httpService.noticeFindById(id).subscribe(data => {
          if (data.status != 200) {
              let toast = this.commonFunc.showInsideErrorToast(data);
              toast.present();
              return;
          }
          this.noticeBean = data.data;
          if (this.noticeBean.fileUrls) {
              this.imageList = this.noticeBean.fileUrls.split(",");
          }
      });
  }

    submit() {
        this.repairInfoBean.resolveDate = moment(new Date()).format(
            "YYYYMMDDHHmmss"
        );
        if (!(this.repairInfoBean.isRepaired == "1")) {
            this.repairInfoBean.safeSignUrl = null;
        }

        this.httpService.updateRepairList(this.repairInfoBean).subscribe(data => {
            if (data.status == 200) {
                window["jQuery"]("#signatureid").remove();
                window["jQuery"]("#showImage").remove();
                //如果未修复，维修人员再次召修
                if(this.repairInfoBean.isRepaired == '0'){
                    this.navCtrl.push(ApplyRepairPage, {
                        applyType: "2",
                        elevatorId: this.repairInfoBean.elevatorId,
                        elevatorNum: this.repairInfoBean.elevatorNum,
                        buildAddr: this.buildAddr
                    });
                }else{
                    window["jQuery"]("#signatureid").remove();
                    window["jQuery"]("#showImage").remove();
                    this.navCtrl.pop();
                }
            }
        });



        //不管是已修复还是未修复，都更新消息为已读
        if (this.repairInfoBean.isRepaired == "1" || this.repairInfoBean.isRepaired == "0") {
            let param = {
                id: this.noticeBean.id,
                isRead: "1"
            };
            this.httpService.updateReadState(param).subscribe(data => {
                if (data.status == 200) {
                    const alert1 = this.alertCtrl.create({
                        // title: '',
                        subTitle: "更新成功,已读的消息请到历史中查看",
                        buttons: ["确定"]
                    });
                    alert1.present();
                } else {
                    const alert1 = this.alertCtrl.create({
                        title: "失败",
                        subTitle: "更新消息状态失败，请重试",
                        buttons: ["确定"]
                    });
                    alert1.present();
                }
            });
        }else {
            this.navCtrl.pop();
        }
    }

    getImgUrl(param) {
        this.repairInfoBean.safeSignUrl = param.imageDate;
    }
}

class NoticeDetailBean {
    id: string;
    title: string;
    content: string;
    fileUrls: string;
    sendUserId: string;
    receiveUserId: string;
    isRead: string;
    noticeTime: string;
    receivePartyId: string;
    elevatoriType: string;
    elevatoriMonId: string;
    repairInfoId: string;
}
